import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        cb = QToolButton(self)   
        cb.setToolTip('Default ToolButton')            
        cb.clicked.connect(self.OnClicked)
        layout.addWidget(cb)

        groupBox = QGroupBox('ToolButton With Icon')
        groupLayout = QHBoxLayout()
        groupLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        cb = QToolButton(self)        
        cb.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/confirm.png'))
        cb.clicked.connect(self.OnIconClicked)
        groupLayout.addWidget(cb)

        cb = QToolButton(self)
        cb.setText('Confirm')
        cb.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/confirm.png'))
        cb.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
        cb.clicked.connect(self.OnIconClicked)
        groupLayout.addWidget(cb)

        cb = QToolButton(self)
        cb.setText('Save')
        cb.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/confirm.png'))
        cb.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
        cb.setPopupMode(QToolButton.ToolButtonPopupMode.MenuButtonPopup)
        self.menu = QMenu(cb)
        self.menu.addAction(QAction("Save", self.menu, triggered=self.OnSaveActionClicked))
        self.menu.addAction(QAction('Save as',self.menu,triggered=self.OnSaveasActionClicked))        
        cb.setMenu(self.menu)
        cb.clicked.connect(self.OnMenuButtonClicked)
        groupLayout.addWidget(cb)
        groupBox.setLayout(groupLayout)
        layout.addWidget(groupBox)

        self.setLayout(layout)

    def OnClicked(self,event):            
        self.parent.OnShowMessage(f'ToolButton Clicked')
    
    def OnIconClicked(self,event):            
        self.parent.OnShowMessage(f'Icon ToolButton Clicked')

    def OnMenuButtonClicked(self,event): 
        self.parent.OnShowMessage(f'Menu ToolButton Clicked')
        self.menu.show()
    
    def OnSaveActionClicked(self,event):        
        self.parent.OnShowMessage(f'Menu Save Action Clicked')
    def OnSaveasActionClicked(self,event):        
        self.parent.OnShowMessage(f'Menu Saveas Action Clicked') 
    
        

def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet